package com.sptpc.learnspring.service;


import com.sptpc.learnspring.model.Category;
import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.repository.DrinkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DrinkService {
    @Autowired
    private DrinkRepository drinkRepository;

    public Drink saveDrink(Drink drink) {
        return drinkRepository.save(drink);
    }
    public List<Drink> getAllDrinks(Long categoryId){
        return drinkRepository.getDrinksByCategory_Id(categoryId);
    }

    public Drink getDrink(String name) {
//                                                          自动创建sql语句通过By
        Optional<Drink> drink = drinkRepository.findDrinksByNameLike(name);
        if (drink.isPresent()) {
            return drink.get();
        }else {
            return null;
        }
    }
}
